//进行axios的封装ß
import axios from 'axios'
//获取token
import { getToken } from '@/utils/token'
const request = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
  },
})
//添加请求与响应拦截器
request.interceptors.request.use((config) => {
  //在发送请求之前做某事
  const token = getToken()
  if (token) {
    config.headers['token'] = token
  }
  return config
})
//添加响应拦截器
request.interceptors.response.use(
  (response) => {
    //在请求成功之后做某事
    return response.data
  },
  (error) => {
    //在请求失败之后做某事
    return Promise.reject(error)
  },
)

export default request
